// Package util @Title util[简述该包的主要作用]
// @Description
// @Author  cnHuaShao
// @since v0.0.1
// @Version v1.0.0
// @Date 2024/4/22 22:53
// @Update 2024/4/22 22:53
package util

import (
	"github.com/mojocn/base64Captcha"
	"image/color"
)

// configJsonBody json request body.
type configJsonBody struct {
	Id            string
	CaptchaType   string
	VerifyValue   string
	DriverAudio   *base64Captcha.DriverAudio
	DriverString  *base64Captcha.DriverString
	DriverChinese *base64Captcha.DriverChinese
	DriverMath    *base64Captcha.DriverMath
	DriverDigit   *base64Captcha.DriverDigit
}

var store = base64Captcha.DefaultMemStore

// GetCaptcha
// @Description: 生成验证码
// @return string 标记号
// @return string 验证码base64
// @return error 错误
func GetCaptcha(captchaType string) (string, string, error) {
	// https://captcha.mojotv.cn/ 调试配置
	var param configJsonBody = configJsonBody{
		Id:          "",
		CaptchaType: captchaType,
		VerifyValue: "",
		// 声音验证码配置
		DriverAudio: &base64Captcha.DriverAudio{
			Length:   6,
			Language: "zh",
		},
		DriverString: &base64Captcha.DriverString{
			Length:          4,                                     // Length随机字符串长度。
			Height:          60,                                    // Height png高度，单位为像素。
			Width:           240,                                   // 验证码png宽度，以像素为单位。
			ShowLineOptions: 2,                                     // 选项显示空线|选项显示粘液线|选项显示正弦线
			NoiseCount:      15,                                    // NoiseCount文本噪声计数。
			Source:          "1234567890abcdefghigklmnpqrstuvwxyz", // Source是unicode，它是来自的rand字符串。
		},
		DriverChinese: &base64Captcha.DriverChinese{
			Height:          60,
			Width:           320,
			ShowLineOptions: 0,
			NoiseCount:      0,
			Source:          "设想,你在,处理,消费者,的音,频输,出音,频可,能无,论什,么都,没有,任何,输出,或者,它可,能是,单声道,立体声,或是,环绕立,体声的,,不想要,的值",
			Length:          2,
			Fonts:           []string{"wqy-microhei.ttc"},
			BgColor:         &color.RGBA{125, 125, 0, 118},
		},
		DriverMath: &base64Captcha.DriverMath{
			Height:          60,                           // Height png高度，单位为像素。
			Width:           240,                          // 验证码png宽度，以像素为单位。
			ShowLineOptions: 0,                            // 选项显示空线|选项显示粘液线|选项显示正弦线
			NoiseCount:      0,                            // 文本噪声计数。
			Fonts:           []string{"wqy-microhei.ttc"}, // 按名称加载字体参见Fonts
			BgColor:         &color.RGBA{0, 0, 0, 0},      // 验证码图像背景颜色(可选)
		},
		// 数字验证码配置
		DriverDigit: &base64Captcha.DriverDigit{
			Height:   80,
			Width:    240,
			Length:   5,
			MaxSkew:  0.7,
			DotCount: 80,
		},
	}
	var driver base64Captcha.Driver

	// 创建base64编码验证码
	switch param.CaptchaType {
	case "audio":
		driver = param.DriverAudio
	case "string":
		driver = param.DriverString.ConvertFonts()
	case "math":
		driver = param.DriverMath.ConvertFonts()
	case "chinese":
		driver = param.DriverChinese.ConvertFonts()
	default:
		driver = param.DriverDigit
	}
	c := base64Captcha.NewCaptcha(driver, store)

	return c.Generate()
}

// VerifyCaptcha
// @Description: base64Captcha 验证
// @param id
// @param VerifyValue
// @return bool
func VerifyCaptcha(id, VerifyValue string) bool {
	return store.Verify(id, VerifyValue, true)
}
